<?php
//
// +----------------------------------------------------------------------+
// |zen-cart Open Source E-commerce                                       |
// +----------------------------------------------------------------------+
// | Copyright (c) 2003 The zen-cart developers                           |
// |                                                                      |
// | http://www.zen-cart.com/index.php                                    |
// |                                                                      |
// | Portions Copyright (c) 2003 osCommerce                               |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.0 of the GPL license,       |
// | that is bundled with this package in the file LICENSE, and is        |
// | available through the world-wide-web at the following url:           |
// | http://www.zen-cart.com/license/2_0.txt.                             |
// | If you did not receive a copy of the zen-cart license and are unable |
// | to obtain it through the world-wide-web, please send a note to       |
// | license@zen-cart.com so we can mail you a copy immediately.          |
// +----------------------------------------------------------------------+
//
// ===============================================================================
// Add-On: All Invoices v1.0
// Designed for: Zen Cart 1.3.x series
// Created by: Mathew O'Marah (www.mdodesign.co.uk)
//
// Donations:  Please support Zen Cart!  paypal@zen-cart.com  - Thank you!
// ===============================================================================

require('includes/application_top.php');

global $db;

//update expiration date is null when product is out of stock
$db->Execute("update " . TABLE_PRODUCTS . " set products_expiration_date = null where products_quantity <= 0;");

function dateDiff($start, $end) {

	$start_ts = strtotime($start);

	$end_ts = strtotime($end);

	$diff = $end_ts - $start_ts;

	return round($diff / 86400);

}
?>

<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
<html <?php echo HTML_PARAMS; ?>>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?php echo CHARSET; ?>">
<title><?php echo TITLE; ?></title>
<link rel="stylesheet" type="text/css" href="includes/stylesheet.css">
<link rel="stylesheet" type="text/css" href="includes/cssjsmenuhover.css" media="all" id="hoverJS">
<script language="javascript" src="includes/menu.js"></script>
<script language="javascript" src="includes/general.js"></script>
<script type="text/javascript">
  <!--
  function init()
  {
    cssjsmenu('navbar');
    if (document.getElementById)
    {
      var kill = document.getElementById('hoverJS');
      kill.disabled = true;
    }
  }
  // -->
</script>
</head>
<body onLoad="init()">
<!-- header //-->
<?php require(DIR_WS_INCLUDES . 'header.php'); ?>
<!-- header_eof //-->

<!-- body //-->
<table border="0" width="100%" cellspacing="2" cellpadding="2">
  <tr>
<!-- body_text //-->

    <td width="100%" valign="top"><table border="0" width="100%" cellspacing="0" cellpadding="2">
      <tr>
        <td><table border="0" width="100%" cellspacing="0" cellpadding="0">
          <tr>
            <td class="pageHeading">CONTROLO VALIDADES</td>
            <td class="pageHeading" align="right"><?php echo zen_draw_separator('pixel_trans.gif', HEADING_IMAGE_WIDTH, HEADING_IMAGE_HEIGHT); ?></td>
          </tr>
        </table></td>
      </tr>
      <tr>
        <td>
          <table width="100%" cellspacing="0" cellpadding="2" border="0">
             <tbody><tr class="dataTableHeadingRow">
                <td class="dataTableHeadingContent" width="15%">Product Model</td>
                <td class="dataTableHeadingContent">Product Name</td>
                <td class="dataTableHeadingContent">Product Quantity</td>
                <td class="dataTableHeadingContent" width="10%">Expiration Date</td>
              </tr>
      	<?php
      		
			$sql = "SELECT p.products_model, p.products_quantity, p.products_expiration_date products_expiration_date_full, date_format(p.products_expiration_date, '%m/%Y') as products_expiration_date, d.products_name 
					FROM " . TABLE_PRODUCTS . " p INNER JOIN " . TABLE_PRODUCTS_DESCRIPTION . " d ON p.products_id=d.products_id and d.language_id='" . $_SESSION['languages_id'] . "'
					WHERE p.products_quantity > 0 AND p.products_id not in (select products_id from " . TABLE_PRODUCTS_TO_CATEGORIES . " where categories_id in (121,122))
					ORDER BY IFNULL( p.products_expiration_date,  '9999-12-31' )";
			
			//echo($sql);exit();
			$products = $db->Execute($sql);		
			
			while (!$products->EOF) {				
				$products_name = $products->fields["products_name"];
				$products_quantity = $products->fields["products_quantity"];								
				$products_model = $products->fields["products_model"];				
				$products_expiration_date = $products->fields["products_expiration_date"];
				$products_expiration_date_full = $products->fields["products_expiration_date_full"];
				$bg = "none";
				if(isset($products_expiration_date_full))
				{
					$d = date('Y-m-d h:i:s', time());
					
					$dateDiff = dateDiff($d, $products_expiration_date_full);				
									
					if($dateDiff < 60)
						$bg = "red";
					else if($dateDiff < 120)
						$bg = "yellow";
				}
				
				$bg_name = "none";
				
				if($products_quantity > 0 && !isset($products_expiration_date_full))
					$bg_name = "yellow";
		?>
			  <tr class="dataTableRow" style="background-color:<?php echo $bg;?>">
                <td class="dataTableContent"><?php echo $products_model;?></td>
                <td class="dataTableContent" style="background-color: <?php echo $bg_name;?>"><?php echo $products_name;?></td>
                <td class="dataTableContent"><?php echo $products_quantity;?></td>
                <td class="dataTableContent"><?php echo $products_expiration_date;?></td>
               </tr>
		<?php 
				$products->MoveNext();
			}
		?>
             </tbody>
          </table>
        </td>
      </tr>
    </table></td>

<!-- body_text_eof //-->
  </tr>
</table>
<!-- body_eof //-->

<!-- footer //-->
<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
<!-- footer_eof //-->
<br>
</body>
</html>
<?php

require(DIR_WS_INCLUDES . 'application_bottom.php');
?>